home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_11 / phillip2 / segment2.c < prev    next >
C/C++ Source or Header  |  1993-05-14  |  27KB  |  932 lines

  1.  
  2.  
  3.  
  4.        /***********************************************
  5.        *
  6.        *       file d:\cips\segment2.c
  7.        *
  8.        *       Functions: This file contains
  9.        *          find_cutoff_point
  10.        *          edge_region
  11.        *          gray_shade_region
  12.        *          edge_gray_shade_region
  13.        *          pixel_grow
  14.        *          pixel_label_and_check_neighbors
  15.        *          is_close
  16.        *          erode_image_array
  17.        *          get_edge_region_options
  18.        *
  19.        *       Purpose:
  20.        *          These function implement the three
  21.        *          segmentation techniques in Image
  22.        *          Processing part 10.
  23.        *
  24.        *       External Calls:
  25.        *          wtiff.c - round_off_image_size
  26.        *                    create_file_if_needed
  27.        *                    write_array_into_tiff_image
  28.        *          tiff.c - read_tiff_header
  29.        *          rtiff.c - read_tiff_image
  30.        *          numcvrt.c - get_integer
  31.        *          edges.c - quick_edge
  32.        *                    homogeneity
  33.        *                    difference_edge
  34.        *                    contrast_edge
  35.        *                    gaussian_edge
  36.        *                    range
  37.        *                    variance
  38.        *                    detect_edges
  39.        *          hist.c - calculate_histogram
  40.        *                   zero_histogram
  41.        *          thresh.c - threshold_image_array
  42.        *
  43.        *       Modifications:
  44.        *          5 December 1992 - created
  45.        *
  46.        *************************************************/
  47.  
  48. #include "cips.h"
  49.  
  50.  
  51.  
  52.      /*******************************************
  53.      *
  54.      *   find_cutoff_point(..
  55.      *
  56.      *   This function looks at a histogram
  57.      *   and sets a cuttoff point at a given
  58.      *   percentage of pixels.
  59.      *   For example, if percent=0.6, you
  60.      *   start at 0 in the histogram and count
  61.      *   up until you've hit 60% of the pixels.
  62.      *   Then you stop and return that pixel
  63.      *   value.
  64.      *
  65.      ********************************************/
  66.  
  67. find_cutoff_point(histogram, percent, cutoff)
  68.    unsigned long histogram[];
  69.    float    percent;
  70.    short    *cutoff;
  71. {
  72.    float  fd, fsum, sum_div;
  73.    int    i, looking;
  74.    long   lc, lr, num=0, sum=0;
  75.  
  76.    sum     = 0;
  77.    i       = 0;
  78.    lr      = (long)(ROWS);
  79.    lc      = (long)(COLS);
  80.    num     = lr*lc;
  81.    fd      = (float)(num);
  82.  
  83.    while(looking){
  84.       fsum    = (float)(sum);
  85.       sum_div = fsum/fd;
  86.       if(sum_div >= percent)
  87.          looking = 0;
  88.       else
  89.          sum = sum + histogram[i++];
  90.    }  /* ends while looking */
  91.  
  92.    if(i >= 256) i = 255;
  93.    *cutoff = i;
  94.    printf("\nCutoff is %d sum=%ld", *cutoff, sum);
  95. }  /* ends find_cutoff_point */
  96.  
  97.  
  98.  
  99.  
  100.  
  101.      /*******************************************
  102.      *
  103.      *   edge_region(..
  104.      *
  105.      *   This function segments an image by
  106.      *   growing regions inside of edges.
  107.      *   The steps are:
  108.      *      . detect edges
  109.      *      . threshold edge output to a
  110.      *        percent value
  111.      *      . remove edges from consideration
  112.      *      . grow regions
  113.      *
  114.      *******************************************/
  115.  
  116.  
  117. edge_region(in_name, out_name, the_image, out_image,
  118.             il, ie, ll, le, edge_type, min_area,
  119.             max_area, diff, percent, set_value,
  120.             erode)
  121.    char     in_name[], out_name[];
  122.    float    percent;
  123.    int      edge_type, il, ie, ll, le;
  124.    short    diff, erode, 
  125.             max_area, min_area, 
  126.             set_value,
  127.             the_image[ROWS][COLS],
  128.             out_image[ROWS][COLS];
  129. {
  130.  
  131.    int    a, b, count, i, j, k,
  132.           length, width;
  133.    short  cutoff;
  134.    struct tiff_header_struct image_header;
  135.    unsigned long histogram[GRAY_LEVELS+1];
  136.  
  137.  
  138.    create_file_if_needed(in_name, out_name, out_image);
  139.  
  140.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  141.  
  142.       /***************************
  143.       *
  144.       *   Detect the edges.  Do
  145.       *   not threshold.
  146.       *
  147.       ****************************/
  148.  
  149.    if(edge_type == 1  ||
  150.       edge_type == 2  ||
  151.       edge_type == 3)
  152.       detect_edges(in_name, out_name, the_image,
  153.                    out_image, il, ie, ll, le,
  154.                    edge_type, 0, 0);
  155.  
  156.    if(edge_type == 4){
  157.       quick_edge(in_name, out_name, the_image,
  158.                  out_image, il, ie, ll, le,
  159.                  0, 0);
  160.    }  /* ends if 4 */
  161.  
  162.    if(edge_type == 5){
  163.       homogeneity(in_name, out_name, the_image,
  164.                  out_image, il, ie, ll, le,
  165.                  0, 0);
  166.    }  /* ends if 5 */
  167.  
  168.    if(edge_type == 6){
  169.       difference_edge(in_name, out_name, the_image,
  170.                  out_image, il, ie, ll, le,
  171.                  0, 0);
  172.    }  /* ends if 6 */
  173.  
  174.    if(edge_type == 7){
  175.       contrast_edge(in_name, out_name, the_image,
  176.                  out_image, il, ie, ll, le,
  177.                  0, 0);
  178.    }  /* ends if 7 */
  179.  
  180.    if(edge_type == 8){
  181.       gaussian_edge(in_name, out_name, the_image,
  182.                  out_image, il, ie, ll, le,
  183.                  3, 0, 0);
  184.    }  /* ends if 8 */
  185.  
  186.    if(edge_type == 10){
  187.       range(in_name, out_name, the_image,
  188.             out_image, il, ie, ll, le,
  189.             3, 0, 0);
  190.    }  /* ends if 10 */
  191.  
  192.    if(edge_type == 11){
  193.       variance(in_name, out_name, the_image,
  194.                out_image, il, ie, ll, le,
  195.                0, 0);
  196.    }  /* ends if 11 */
  197.  
  198. /**write_array_into_tiff_image("f:e1.tif", out_image,
  199. il, ie, ll, le);**/
  200.  
  201.       /* copy out_image to the_image */
  202.    for(i=0; i<ROWS; i++)
  203.       for(j=0; j<COLS; j++)
  204.          the_image[i][j] = out_image[i][j];
  205.  
  206.       /******************************
  207.       *
  208.       *   Threshold the edge detector
  209.       *   output at a given percent.
  210.       *   This eliminates the weak
  211.       *   edges.
  212.       *
  213.       *******************************/
  214.    zero_histogram(histogram);
  215.    calculate_histogram(the_image, histogram);
  216.    find_cutoff_point(histogram, percent, &cutoff);
  217.    threshold_image_array(the_image, out_image,
  218.                          255, cutoff, set_value);
  219. /**write_array_into_tiff_image("f:e2.tif", out_image,
  220. il, ie, ll, le);**/
  221.  
  222.    if(erode != 0){
  223.          /* copy out_image to the_image */
  224.       for(i=0; i<ROWS; i++)
  225.          for(j=0; j<COLS; j++)
  226.             the_image[i][j] = out_image[i][j];
  227.       erode_image_array(the_image, out_image,
  228.                         set_value, erode);
  229.    }  /* ends if erode */
  230.  
  231. /**write_array_into_tiff_image("f:e3.tif", out_image,
  232. il, ie, ll, le);**/
  233.  
  234.       /*******************************
  235.       *
  236.       *   Set all the edge values to
  237.       *   FORGET_IT so the region
  238.       *   growing will not use those
  239.       *   points.
  240.       *
  241.       *******************************/
  242.  
  243.    for(i=0; i<ROWS; i++)
  244.       for(j=0; j<COLS; j++)
  245.          if(out_image[i][j] == set_value)
  246.             out_image[i][j] = FORGET_IT;
  247.  
  248.    for(i=0; i<ROWS; i++)
  249.       for(j=0; j<COLS; j++)
  250.          the_image[i][j] = out_image[i][j];
  251.  
  252.    pixel_grow(the_image, out_image, diff,
  253.               min_area, max_area);
  254.  
  255.    write_array_into_tiff_image(out_name, out_image,
  256.                                il, ie, ll, le);
  257.  
  258. }  /* ends edge_region */
  259.  
  260.  
  261.  
  262.      /*******************************************
  263.      *
  264.      *   gray_shade_region(...
  265.      *
  266.      *   This function segments an image by
  267.      *   growing regions based only on gray
  268.      *   shade.
  269.      *
  270.      *******************************************/
  271.  
  272.  
  273. gray_shade_region(in_name, out_name, the_image,
  274.                   out_image, il, ie, ll, le,
  275.                   diff, min_area, max_area)
  276.    char   in_name[], out_name[];
  277.    int    il, ie, ll, le;
  278.    short  the_image[ROWS][COLS],
  279.           out_image[ROWS][COLS],
  280.           diff, min_area, max_area;
  281. {
  282.    int    a, b, big_count, count, i, j, k, l,
  283.           not_finished, length, width;
  284.    short  temp[3][3];
  285.    struct tiff_header_struct image_header;
  286.  
  287.    create_file_if_needed(in_name, out_name, out_image);
  288.  
  289.    read_tiff_image(in